package chat.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

import chat.client.ClientMain;
import chat.user.Message;
import chat.user.MessageCreator;
import chat.user.Request;
import chat.user.User;

public class ChatPanel extends JPanel {

	private JScrollPane scroll_panel, mes_panel;
	private TabbedPanel tab_panel; 
	private JEditorPane mes_area;
	private JTextArea chat_area = new JTextArea();
	private JTextArea chat_area2 = new JTextArea();
	public User user;
	private ChatHandler chatHandler;
	private List<MessageHandler> hand_list = new LinkedList<MessageHandler>();
	public JToggleButton underline, bold, italic;
	
	public ChatPanel(User u) {

		chatHandler = new ChatHandler();
		new Thread(chatHandler).start();
		Dimension d = new Dimension();
//		d.setSize(400, 400);
//		setSize(d);
		
		setVisible(true);
		JLabel label = new JLabel("This is a default screen");
		//label.setBounds(30, 0, 150, 150);
		
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		
		// add(label);

		this.user = u;

		SoftBevelBorder softborder = new SoftBevelBorder(BevelBorder.RAISED);
		EmptyBorder etched = new EmptyBorder(5, 5, 5, 5);
		CompoundBorder compborder = new CompoundBorder(etched, softborder);
		
		tab_panel = new TabbedPanel();
		
		addContact("General");
		//tab_panel.addNewTab("General");
		add(tab_panel, new GridBagConstraints(0, 0, 4, 1, 3.0, 5.0,
					GridBagConstraints.CENTER,
			       GridBagConstraints.BOTH,
			       new Insets(0, 0, 0, 0), 0, 0));
		//tab_panel.setSize(new Dimension(500,500));
		tab_panel.setBorder(compborder);
		
		bold = new JToggleButton("<html><font size='3'><b>B</b></font></html>");
		add(bold, new GridBagConstraints(0, 1, 1, 1, -13.0, 0.0,
			       GridBagConstraints.LINE_START,
			       GridBagConstraints.LINE_START,
			       new Insets(0, 0, 0, 0), 0, 0));
		
		italic = new JToggleButton("<html><font size='3'><i>I</i></font></html>");
		add(italic, new GridBagConstraints(1, 1, 1, 1, 20.0, 0.0,
			       GridBagConstraints.LINE_START,
			       GridBagConstraints.LINE_START,
			       new Insets(0, 0, 0, 0), 0, 0));
		
		underline = new JToggleButton("<html><font size='3'><u>U</u></font></html>");
		add(underline, new GridBagConstraints(2, 1, 1, 1, 6000.0, 0.0,
			       GridBagConstraints.LINE_START,
			       GridBagConstraints.LINE_START,
			       new Insets(0, 0, 0, 0), 0, 0));
		
		JButton send_btn = new JButton("SEND");
		send_btn.setSize(new Dimension(30,30));
		send_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String text = mes_area.getText();
				mes_area.setText("");

				if (!text.equalsIgnoreCase("")) {
					
					int index = tab_panel.getSelectedIndex();
					
					chatHandler.setTextFromMessageToGroup(text
							,ChatPanel.this
							,tab_panel.getTitleAt(index)
							,user.getUserName());
					
					Request r = MessageCreator.createTextMessage(text, user, tab_panel.getTitleAt(index));
					ClientMain.addOutMessage(r);
				}
			}
		});
		add(send_btn, new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0,
			       GridBagConstraints.EAST,
			       GridBagConstraints.CENTER,
			       new Insets(0, 0, 0, 0), 0, 0));
				
		mes_area = new JEditorPane();
		mes_panel = new JScrollPane(mes_area);

		mes_panel.setBorder(compborder);
		add(mes_panel,new GridBagConstraints(0, 2, 4, 1, 3.0, 1.0,
			       GridBagConstraints.EAST,
			       GridBagConstraints.BOTH,
			       new Insets(0, 0, 0, 0), 0, 0));
	//	mes_panel.setSize(new Dimension(400,100));
		
	}

	public void setChatText(String text, String group_name) {
		
		JEditorPane textArea = tab_panel.getTextArea(group_name);
		
		
		
		//Font font = new Font("Verdana", Font.BOLD, 11);
		//textArea.setFont(font);
		//textArea.setForeground(Color.BLUE);
		textArea.setContentType("text/html");
		textArea.setText(text);
	}

	public String getChatText(String group_name) {
		return tab_panel.getTextArea(group_name).getText();
	}
	
	public String getContactMessages(String contact_name){
		String mesBuffer="";
		for (MessageHandler h: hand_list){
			if (h.getContactMessageName().equalsIgnoreCase(contact_name)){
				mesBuffer = h.getMessages();
			}
		}
		return mesBuffer;
	}
	
	public TabbedPanel getTabbedPanel(){
		return this.tab_panel;
	}
	
	public void addContactMessage(String message, String contact_name){
		for (MessageHandler h: hand_list){
			if (h.getContactMessageName().equalsIgnoreCase(contact_name)){
				h.addMessage(message);
			}
		}
	}
	
	public void addContact(String contact_name){
		this.tab_panel.addNewTab(contact_name);
		boolean flag=false;
		for (MessageHandler mh: hand_list){
			if (mh.getContactMessageName().equalsIgnoreCase(contact_name)){
				flag = true;
			}
		}
		
		if (!flag) hand_list.add(new MessageHandler(contact_name));
	}
	
	public void removeContact(String contact_name){
		this.tab_panel.removeTextArea(contact_name);
		for (MessageHandler mh: hand_list){
			if (mh.getContactMessageName().equalsIgnoreCase(contact_name)){
				hand_list.remove(contact_name);
			}
		}
		
	}
	
	public JScrollPane getScrollPanel(){
		return this.mes_panel;
	}
	
	public ChatHandler getChatHandler(){
		return this.chatHandler;
	}
	

	/*
	 * public static void main (String []args){
	 * 
	 * JFrame frame = new JFrame(); ChatPanel panel = new ChatPanel();
	 * frame.add(panel); frame.setBounds(0, 0, 500, 500);
	 * 
	 * frame.setVisible(true);
	 * frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
	 */
}